package org.ruanjj.removeelement;

public class Solution {
    public int removeElement(int[] nums, int val) {
        int i = 0;
        int n = nums.length;
        int j = n-1;
        int count = 0;

        while(j >= i){
            if(nums[j] == val){
                nums[j] = -1;
                count++;
                j--;
            }else{
                if(nums[i] == val){
                    nums[i] = nums[j];
                    nums[j] = -1;
                    count++;
                    j--;
                }
                i++;
            }
        }
        return n-count;
    }
}
